<?php
  $qqq = 0;
  include('header.php');
  if ($_SESSION['user_level'] < 1) {
      header('location: signin.php');
  }
  
  if (mysql_real_escape_string($_GET['move'] != '') and $_SESSION['user_level'] >= 1 and $_SESSION['signed_in']) {
      if ($_SERVER['REQUEST_METHOD'] != 'POST') {
          $result = $db->query("SELECT * FROM " . $table_prefix . "categories");
          echo '<br /><form name="myForm" method="post" action=""><b>  &nbsp; &nbsp;<br /> <b>&nbsp; &nbsp;Please select a destination forum:</b><br>&nbsp; &nbsp;</input>';
          echo '<select name="topic_cat">';
          while ($row = $db->fetch_array($result,'assoc')) {
              if ($row['cat_id'] == $_GET['f']) {
                  $selected = "selected";
              } else {
                  
                  $selected = null;
              }
              if ($row['cat_parent'] == 0) {
                  echo '<option disabled="disabled" value="' . $row['cat_id'] . '">' . $row['cat_name'] . '</option>';
                  
                  $qqq += 1;
                  $result2 = $db->query("SELECT * FROM " . $table_prefix . "categories WHERE cat_parent >0");
              }
              while ($row2 = $db->fetch_array($result2,'assoc')) {
                  if ($row2['cat_parent'] == $row['cat_id']) {
                      if ($row2['cat_id'] == $_GET['f']) {
                          $selected = "selected";
                      } else {
                          
                          $selected = null;
                      }
                      if ($row2["cat_id"] == mysql_real_escape_string($_GET['move'])) {
                          $selected = 'disabled="disabled"';
                      }
                      echo '<option ' . $selected . ' value = ' . $row2["cat_id"] . '>&nbsp;|- > ' . $row2["cat_name"] . '</option>';
                  }
              }
          }
          echo '</select><br /><br />  &nbsp; &nbsp;<input type="submit" class="inputButton" value="Move topic" />
 </input>';
      }
      if ($_SERVER['REQUEST_METHOD'] == 'POST') {
          $move = $db->query("UPDATE " . $table_prefix . "topics SET topic_cat = " . $_POST["topic_cat"] . " WHERE topic_id = " . mysql_real_escape_string($_GET['t']) . "") or die(mysql_error());
          $posts_c = $db->query("UPDATE " . $table_prefix . "posts SET post_cat = " . $_POST["topic_cat"] . " WHERE post_cat =" . mysql_real_escape_string($_GET['move']) . "") or die(mysql_error());
          
          if ($move and $posts_c) {
		  //SYNCH LAST POSTS
		  $sync = $db->query("SELECT 
		  topic_id, 
		  topic_subject, 
		  topic_last_poster, 
		  topic_last_poster_id,
		  topic_last_post_date FROM " . $table_prefix . "topics
		  WHERE
		  topic_cat = ".mysql_real_escape_string($_GET['move'])."
		  ORDER BY topic_last_post_date DESC LIMIT 1
		  ");
		  //TOPICS IS MOVED...CHECK IF THERE IS ANOTHER TOPIC...
		  if (mysql_num_rows($sync)>0)
		  {$sync_lastp = $db->fetch_array($sync,'assoc');
		  $last_post_subject = trim($sync_lastp['topic_subject']);
		  $last_post_id = $sync_lastp['topic_id'];
		  $last_poster_id = $sync_lastp['topic_last_poster_id'];
		  $last_post_time = $sync_lastp['topic_last_post_date'];
		  $last_poster_name = $sync_lastp['topic_last_poster'];
		  }
		  //IF NOT SET 'no topics'
		  else
		  {
		  $last_post_subject = "";
		  $last_post_id = "0";
		  $last_poster_id = "0";
		  $last_post_time = "0000-00-00 00:00:00";
		  $last_poster_name = "";
		  }
		  $upd = $db->query("UPDATE " . $table_prefix . "categories SET
		  cat_last_post_id = $last_post_id,
		  cat_last_poster_id = $last_poster_id,
		  cat_last_post_subject = '$last_post_subject',
		  cat_last_post_time = '$last_post_time',
		  cat_last_poster_name = '$last_poster_name' 
		  WHERE cat_id = ".mysql_real_escape_string($_GET['move'])."
		   LIMIT 1");
		  
		  $sync2 = $db->query("SELECT 
		  topic_id, 
		  topic_subject, 
		  topic_last_poster, 
		  topic_last_poster_id,
		  topic_last_post_date FROM " . $table_prefix . "topics
		  WHERE
		  topic_cat = ".mysql_real_escape_string($_POST["topic_cat"])."
		  ORDER BY topic_last_post_date DESC LIMIT 1
		  ");
		  //TOPICS IS MOVED...CHECK IF THERE IS ANOTHER TOPIC...
		  if (mysql_num_rows($sync2)>0)
		  {$sync_lastp2 = $db->fetch_array($sync2,'assoc');
		  $last_post_subject2 = trim($sync_lastp2['topic_subject']);
		  $last_post_id2 = $sync_lastp2['topic_id'];
		  $last_poster_id2 = $sync_lastp2['topic_last_poster_id'];
		  $last_post_time2 = $sync_lastp2['topic_last_post_date'];
		  $last_poster_name2 = $sync_lastp2['topic_last_poster'];
		  }
		  //IF NOT SET 'no topics'
		  else
		  {
		  $last_post_subject2 = "";
		  $last_post_id2 = "0";
		  $last_poster_id2 = "0";
		  $last_post_time2 = "0000-00-00 00:00:00";
		  $last_poster_name2 = "";
		  }
		  $upd2 = $db->query("UPDATE " . $table_prefix . "categories SET
		  cat_last_post_id = $last_post_id2,
		  cat_last_poster_id = $last_poster_id2,
		  cat_last_post_subject = '$last_post_subject2',
		  cat_last_post_time = '$last_post_time2',
		  cat_last_poster_name = '$last_poster_name2' 
		  WHERE cat_id = ".mysql_real_escape_string($_POST["topic_cat"])."
		   LIMIT 1");
		  //END SYNCH LAST POSTS
		  //TOTAL 8 QUERIES
              echo "<hr/><br><div align='center'>The selected topic has been moved successfully.<br><br><a href='topic.php?t=" . mysql_real_escape_string($_GET['t']) . "'/>Return to the previous page</a><br><br><a href='category.php?f=" . $_POST["topic_cat"] . "'/>Go to new forum</a></div><br/><br/>";
          }
      }
  }

  $pageContents = ob_get_contents();
  ob_end_clean();
  echo str_replace('<!--TITLE-->', $pageTitle, $pageContents);
?>